package com.hongdee.scm.warningsetup.service.impl;

import com.hongdee.common.util.TokenUtil;
import com.hongdee.scm.common.service.impl.Common;
import com.hongdee.scm.warningsetup.dao.ITScmWarningSetUpDao;
import com.hongdee.scm.warningsetup.entity.TScmWarningSetUp;
import com.hongdee.scm.warningsetup.service.ITScmWarningSetUpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * 名  称:TScmWarningServiceImpl
 * 类  型:class
 * 描  述:
 * 路  径:com.hongdee.scm.warningsetup.service.impl
 * 作  者: 术洪超
 * 日  期:2017-06-11 14:16
 */
@Service
public class TScmWarningSetUpServiceImpl implements ITScmWarningSetUpService {

    String tabName = "T_SCM_WARNING_SETUP";
    String entityName = "TScmWarningSetUp";

    @Autowired
    private ITScmWarningSetUpDao itScmWarningSetUpDao;


    @Override
    public Map info(String id) throws Exception {

        StringBuffer sql = new StringBuffer();
        Common common = new Common();
        Boolean hasWhere = false;
        List params = new ArrayList();
        sql.append("select * from " + tabName);
        if (!StringUtils.isEmpty(id)) {
            hasWhere = common.appendWhereIfNeed(sql, hasWhere);
            sql.append(" id = ?");
            params.add(id);
        }
        return this.itScmWarningSetUpDao.info(sql.toString(), params.toArray());
    }

    @Override
    public TScmWarningSetUp save(TScmWarningSetUp data, String uid) throws Exception {

        if (data.getId() == null) {
            data.setCreateUser(uid);
            data.setCreateTime(Calendar.getInstance().getTimeInMillis());

        } else {
            data.setUpdateUser(uid);
            data.setUpdateTime(Calendar.getInstance().getTimeInMillis());
        }
        data.setCompanyId(TokenUtil.getCompanyId(uid));
        this.itScmWarningSetUpDao.saveOrUpdate(data);
        return data;
    }

    @Override
    public void update(String id, String status) throws Exception {

    }

    @Override
    public Map page(String start, String limit, String type, String levelId, String uid) throws Exception {


        StringBuffer sql = new StringBuffer();
        List params = new ArrayList();
        sql.append("select ws.*,b.b_name,m.model_name from " + tabName + " ws left join t_bc_brand b on ws.level_id = b.b_code left join t_bc_car_series s on s.id = ws.level_id left join t_bc_carmodel m on m.id = ws.level_id");

        sql.append(" where ws.company_id = ?");
        params.add(TokenUtil.getCompanyId(uid));

        if (!StringUtils.isEmpty(type)) {
            sql.append(" and ws.level_type = ?");
            params.add(type);
        }

        if (!StringUtils.isEmpty(levelId)) {
            sql.append(" and ws.level_id = ?");
            params.add(levelId);
        }
        return this.itScmWarningSetUpDao.page(sql.toString(), params.toArray(), start, limit);
    }
}
